package com.ihr360.cnb.module.payroll.controller;

import com.ihr360.cnb.module.payroll.service.PayrollAggregateService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.request.attendance.AttendanceTakeNumberRequest;
import com.ihr360.payroll.type.AttendanceState;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Eric
 */
@RestController
@RequestMapping("/attendance")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_NAME,
        hasExtendsAbstractRestController = false)
public class AttendanceAggregateController {

    @Autowired
    private PayrollAggregateService payrollAggregateService;

    /**
     * 考勤汇总同步取数
     */
    @FunctionDefinition(functionName = "考勤汇总同步取数", functionCode = ModuleCodeStore.Function.ATTENDANCE_TAKE_NUMBER)
    @ApiOperation(value = "/takeNumber", httpMethod = "POST", notes = "考勤汇总取数方式")
    @PostMapping(value = "/takeNumber")
    public void takeNumber(AuthUserInfo userInfo, @RequestBody AttendanceTakeNumberRequest request) {
        payrollAggregateService.takeNumber(userInfo, request);
    }

    /**
     * 考勤锁定接口
     */
    @FunctionDefinition(functionName = "考勤锁定接口", functionCode = ModuleCodeStore.Function.ATTENDANCE_LOCK)
    @ApiOperation(value = "/lockAttendance", httpMethod = "POST", notes = "考勤锁定接口")
    @GetMapping(value = "/lockAttendance")
    public void lockAttendanceState(AuthUserInfo userInfo, @RequestParam Long summaryId, @RequestParam AttendanceState state) {
        if (summaryId == null || state == null) {
            throw new Ihr360Exception(null, "汇总信息状态不可为空");
        }
        payrollAggregateService.lockAttendanceState(userInfo, summaryId, state);
    }

    /**
     * 考勤未审批单据查询
     */
    @FunctionDefinition(functionName = "考勤未审批单据查询", functionCode = ModuleCodeStore.Function.ATTENDANCE_TAKE_NUMBER)
    @ApiOperation(value = "/getUnapprovedCount", httpMethod = "POST", notes = "考勤未审批单据查询")
    @PostMapping(value = "/getUnapprovedCount")
    public Map<String, Integer> getUnapprovedCount(AuthUserInfo userInfo, @RequestBody AttendanceTakeNumberRequest request) {
        return payrollAggregateService.getUnapprovedCount(userInfo.getCompanyId(), request);
    }


}
